﻿package com.riatimes.util
{

	/**
	* @author Nick Wong
	*/
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;

	public class ScrollBarUtil
	{
		private var content:DisplayObjectContainer;
		private var upBtn:MovieClip;
		private var downBtn:MovieClip;
		private var thumb:MovieClip;
		private var track:Sprite;

		private var contentHeight:Number;
		private var scrollBarHeight:Number;

		private var moveAmount:Number=10;

		private var contentStartY:Number=0;
		private var contentStartX:Number=0;

		private var recordY:Number=0;

		public function ScrollBarUtil(upBtn:MovieClip, downBtn:MovieClip, thumb:MovieClip, track:Sprite=null)
		{

			this.upBtn=upBtn;
			this.downBtn=downBtn;
			this.thumb=thumb;
			this.track=track;

			this.thumb.useHandCursor=true;
			this.thumb.buttonMode=true;
		}

		public function initScrollBar(content:DisplayObjectContainer, container:DisplayObjectContainer, contentHeight:Number, scrollBarHeight:Number):void
		{
			removeListener();
			this.content=content;
			this.contentHeight=contentHeight;
			this.scrollBarHeight=scrollBarHeight;

			this.track.height=scrollBarHeight;
			this.downBtn.y=scrollBarHeight - this.downBtn.height;

			var shape:Shape=createShape(0x000000, 0x000000, this.content.width, contentHeight);
			container.addChild(shape);
			shape.x=content.x;
			shape.y=content.y;
			this.content.mask=shape;

			contentStartX=this.content.x;
			contentStartY=this.content.y;

			this.thumb.height=Math.ceil((this.scrollBarHeight - this.upBtn.height - this.downBtn.height) * this.contentHeight / content.height);
			if (this.thumb.height > this.scrollBarHeight)
				this.thumb.height=this.scrollBarHeight;
			else
			{
				addListener();
			}
		}

		private function __mouseClickHandle(e:MouseEvent):void
		{
			switch (e.currentTarget)
			{
				case upBtn:
					this.content.y+=moveAmount;
					if (this.content.y > contentStartY)
						content.y=contentStartY;

					break;
				case downBtn:
					this.content.y-=moveAmount;
					if (this.content.y < (contentStartY - content.height + contentHeight))
						content.y=contentStartY - content.height + contentHeight;
					break;
				case thumb:
					trace(thumb.x);
					thumb.startDrag(false, new Rectangle(Math.ceil(thumb.x), upBtn.height, 0, scrollBarHeight - upBtn.height - downBtn.height - thumb.height));
					thumb.stage.addEventListener(MouseEvent.MOUSE_MOVE, __mouseMoveHandle);
					thumb.stage.addEventListener(MouseEvent.MOUSE_UP, __mouseUpHandle);

					break;
			}
		}

		private function doScroll():void
		{
			if (recordY == 0)
				recordY=upBtn.height;

			var ty:Number=(thumb.y - recordY) / (scrollBarHeight - upBtn.height - downBtn.height - thumb.height);
			this.content.y-=Math.floor(ty * (content.height - contentHeight));
			recordY=thumb.y;

			if (this.content.y > contentStartY)
				content.y=contentStartY;
			if (this.content.y < (contentStartY - content.height + contentHeight))
				content.y=contentStartY - content.height + contentHeight;
		}

		private function __mouseUpHandle(e:MouseEvent):void
		{
			thumb.stage.removeEventListener(MouseEvent.MOUSE_MOVE, __mouseMoveHandle);
			thumb.stage.removeEventListener(MouseEvent.MOUSE_UP, __mouseUpHandle);
			thumb.stopDrag();
		}

		private function __mouseMoveHandle(e:MouseEvent):void
		{
			doScroll();
		}

		private function addListener():void
		{
			upBtn.addEventListener(MouseEvent.CLICK, __mouseClickHandle);
			downBtn.addEventListener(MouseEvent.CLICK, __mouseClickHandle);
			thumb.addEventListener(MouseEvent.MOUSE_DOWN, __mouseClickHandle);
		}

		private function removeListener():void
		{
			upBtn.removeEventListener(MouseEvent.CLICK, __mouseClickHandle);
			downBtn.removeEventListener(MouseEvent.CLICK, __mouseClickHandle);
			thumb.removeEventListener(MouseEvent.MOUSE_DOWN, __mouseClickHandle);
		}

		private function createShape(lineColor:uint, fillColor:uint, width:Number, height:Number):Shape
		{
			var shape:Shape=new Shape();
			shape.graphics.lineStyle(1, lineColor);
			shape.graphics.beginFill(fillColor);
			shape.graphics.drawRect(0, 0, width, height);
			shape.graphics.endFill();
			return shape;
		}

	}

}